package com.settlement.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.model.entity.SysCreditCard;
import com.settlement.system.mapper.SysCreditCardMapper;
import com.settlement.system.service.SysCreditCardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysCreditCardQuery;
import com.settlement.system.model.form.SysCreditCardForm;
import com.settlement.system.model.vo.SysCreditCardImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 信用卡表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-17
 */
@Service
public class SysCreditCardServiceImpl extends ServiceImpl<SysCreditCardMapper, SysCreditCard> implements SysCreditCardService {

    @Override
    public Page<SysCreditCard> getPage(SysCreditCardQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysCreditCard> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysCreditCard(SysCreditCardForm sysCreditCardForm) {
         SysCreditCard sysCreditCard = new SysCreditCard();
         BeanUtils.copyProperties(sysCreditCardForm, sysCreditCard);
         sysCreditCard.setCreateTime(null);
         return this.saveOrUpdate(sysCreditCard);
     }

     @Override
     public SysCreditCardForm getSysCreditCardFormData(Long id) {
         SysCreditCard sysCreditCard = this.getById(id);
         SysCreditCardForm sysCreditCardForm = new SysCreditCardForm();
         BeanUtils.copyProperties(sysCreditCard, sysCreditCardForm);
         return sysCreditCardForm;
     }

     @Override
     public boolean deleteSysCreditCard(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysCreditCardImportVo> voList)  {
        List<String> noList = voList.stream().map(SysCreditCardImportVo::getCardNo).distinct().toList();
        List<SysCreditCard> creditCardList = this.list(new LambdaQueryWrapper<SysCreditCard>().in(SysCreditCard::getCardNo, noList));
        List<SysCreditCard> sysCreditCardList = new ArrayList<>();
        SysCreditCard creditCard;
        for (SysCreditCardImportVo importVo : voList) {
            creditCard = creditCardList.stream().filter(item -> Objects.equals(item.getCardNo(), importVo.getCardNo())).findFirst().orElse(null);
            if(Objects.nonNull(creditCard)){
                creditCard.setMobile(importVo.getMobile());
                creditCard.setUserName(importVo.getUserName());
                creditCard.setValidDate(importVo.getValidDate());
                creditCard.setCvv(importVo.getCvv());
                sysCreditCardList.add(creditCard);
            }else{
                SysCreditCard sysCreditCard = new SysCreditCard();
                BeanUtils.copyProperties(importVo, sysCreditCard);
                sysCreditCardList.add(sysCreditCard);
            }
        }
        this.saveOrUpdateBatch(sysCreditCardList);
    }

    @Override
    public List<SysCreditCardImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysCreditCard> sysCreditCardList = this.getBaseMapper().selectBatchIds(ids);
        List<SysCreditCardImportVo> voList = new ArrayList<>();
        for (SysCreditCard sysCreditCard : sysCreditCardList) {
            SysCreditCardImportVo vo = new SysCreditCardImportVo();
            BeanUtils.copyProperties(sysCreditCard, vo);
            voList.add(vo);
        }
        return voList;
    }
}
